package com.mdwz.lu.ukulelecomposer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.mdwz.lu.ukulelecomposer.R;

public class NewComposition extends Activity {

    EditText etTitle , etComposer , etSongwriter , etKeysignature , etTimesignature;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_composition);

        Button bOK = (Button)findViewById(R.id.new_ok);
        Button bCancel = ( Button )findViewById( R.id.new_cancel);

        etTitle = (EditText)findViewById(R.id.new_title);
        etComposer = (EditText)findViewById(R.id.new_composer);
        etSongwriter = (EditText)findViewById(R.id.new_songwriter);
        etKeysignature = (EditText)findViewById(R.id.new_keysignature);
        etTimesignature = (EditText)findViewById(R.id.new_timesignature);

        bCancel.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(RESULT_CANCELED);
                finish();
            }
        });


        bOK.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String strTitle = etTitle.getText().toString();
                if ( strTitle.isEmpty() ){
                    Toast.makeText( getApplication(), "title can't be empty!",Toast.LENGTH_SHORT ).show();
                    return ;
                }

                String strComposer = etComposer.getText().toString();
                if ( strComposer.isEmpty() ){
                    Toast.makeText( getApplication(), "Composer can't be empty!",Toast.LENGTH_SHORT ).show();
                    return ;
                }

                String strSongwriter = etSongwriter.getText().toString();
                if ( strSongwriter.isEmpty() ){
                    Toast.makeText( getApplication(), "Songwriter can't be empty!",Toast.LENGTH_SHORT ).show();
                    return ;
                }

                String strKeysignature = etKeysignature.getText().toString();
                if ( strKeysignature.isEmpty() ){
                    Toast.makeText( getApplication(), "Keysignature can't be empty!",Toast.LENGTH_SHORT ).show();
                    return ;
                }

                String strTimesignature = etTimesignature.getText().toString();
                if ( strTimesignature.isEmpty() ){
                    Toast.makeText( getApplication(), "Timesignature can't be empty!",Toast.LENGTH_SHORT ).show();
                    return ;
                }

                Intent intent = new Intent();
                intent.putExtra( CompositionManager.intentkeys[0] , strTitle );
                intent.putExtra( CompositionManager.intentkeys[1] , strComposer );
                intent.putExtra( CompositionManager.intentkeys[2] , strSongwriter );
                intent.putExtra( CompositionManager.intentkeys[3] , strKeysignature );
                intent.putExtra( CompositionManager.intentkeys[4] , strTimesignature );

                setResult(RESULT_OK , intent );
                finish();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.new_composition, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        return super.onOptionsItemSelected(item);
    }
}
